#!/usr/bin/env ruby
# frozen_string_literal: true

# Configuration Example for LLM Tracer
# This example shows how to configure OpenTelemetry with different exporters

require "bundler/setup"
require "llm_tracer"

# Example 1: Basic configuration with console exporter
puts "=== Example 1: Basic Console Configuration ==="

OpenTelemetry::SDK.configure do |c|
  c.add_span_processor(
    OpenTelemetry::SDK::Trace::Export::SimpleSpanProcessor.new(
      OpenTelemetry::SDK::Trace::Export::ConsoleSpanExporter.new
    )
  )
end

# Test basic tracing
LlmTracer.workflow(name: "test_workflow", version: "1.0.0") do |span|
  puts "Created span: #{span.name}"
  span.set_attribute("test.attribute", "console_exporter")
end

# Example 2: Configuration with OTLP exporter (for Jaeger, Zipkin, etc.)
puts "\n=== Example 2: OTLP Configuration ==="

# Uncomment and modify these lines to use OTLP exporter
=begin
require "opentelemetry/exporter/otlp"

OpenTelemetry::SDK.configure do |c|
  c.add_span_processor(
    OpenTelemetry::SDK::Trace::Export::BatchSpanProcessor.new(
      OpenTelemetry::Exporter::OTLP::Exporter.new(
        endpoint: "http://localhost:4317", # OTLP gRPC endpoint
        # endpoint: "http://localhost:4318", # OTLP HTTP endpoint
        headers: {
          # Add any required headers (e.g., API keys)
          # "Authorization" => "Bearer your-api-key"
        }
      )
    )
  )
end
=end

# Example 3: Configuration with custom sampling
puts "\n=== Example 3: Custom Sampling Configuration ==="

# Uncomment to use custom sampling
=begin
class CustomSampler
  include OpenTelemetry::Trace::Sampler

  def description
    "Custom sampler for LLM applications"
  end

  def should_sample?(trace_id:, span_id:, parent_context:, links:, name:, kind:, attributes:)
    # Sample all LLM-related spans
    if name.start_with?("genai.")
      OpenTelemetry::Trace::Sampling::Result.new(
        decision: OpenTelemetry::Trace::Sampling::Decision::RECORD_AND_SAMPLE,
        attributes: { "sampler.type" => "custom_llm_sampler" }
      )
    else
      OpenTelemetry::Trace::Sampling::Result.new(
        decision: OpenTelemetry::Trace::Sampling::Decision::DROP,
        attributes: {}
      )
    end
  end
end

OpenTelemetry::SDK.configure do |c|
  c.sampler = CustomSampler.new
  c.add_span_processor(
    OpenTelemetry::SDK::Trace::Export::SimpleSpanProcessor.new(
      OpenTelemetry::SDK::Trace::Export::ConsoleSpanExporter.new
    )
  )
end
=end

# Example 4: Configuration with resource attributes
puts "\n=== Example 4: Resource Attributes Configuration ==="

# Uncomment to add resource attributes
=begin
OpenTelemetry::SDK.configure do |c|
  c.resource = OpenTelemetry::SDK::Resources::Resource.create(
    OpenTelemetry::SemanticConventions::Resource::SERVICE_NAME => "llm_application",
    OpenTelemetry::SemanticConventions::Resource::SERVICE_VERSION => "1.0.0",
    OpenTelemetry::SemanticConventions::Resource::DEPLOYMENT_ENVIRONMENT => "development",
    "custom.resource.team" => "ai_team",
    "custom.resource.application" => "content_generator"
  )

  c.add_span_processor(
    OpenTelemetry::SDK::Trace::Export::SimpleSpanProcessor.new(
      OpenTelemetry::SDK::Trace::Export::ConsoleSpanExporter.new
    )
  )
end
=end

# Example 5: Configuration with multiple exporters
puts "\n=== Example 5: Multiple Exporters Configuration ==="

# Uncomment to use multiple exporters
=begin
OpenTelemetry::SDK.configure do |c|
  # Console exporter for development
  c.add_span_processor(
    OpenTelemetry::SDK::Trace::Export::SimpleSpanProcessor.new(
      OpenTelemetry::SDK::Trace::Export::ConsoleSpanExporter.new
    )
  )

  # OTLP exporter for production monitoring
  c.add_span_processor(
    OpenTelemetry::SDK::Trace::Export::BatchSpanProcessor.new(
      OpenTelemetry::Exporter::OTLP::Exporter.new(
        endpoint: "http://localhost:4317"
      )
    )
  )

  # File exporter for debugging
  c.add_span_processor(
    OpenTelemetry::SDK::Trace::Export::SimpleSpanProcessor.new(
      OpenTelemetry::SDK::Trace::Export::FileSpanExporter.new("traces.json")
    )
  )
end
=end

# Example 6: Environment-based configuration
puts "\n=== Example 6: Environment-based Configuration ==="

def configure_tracing
  case ENV["TRACING_ENV"]
  when "development"
    # Development: Console exporter only
    OpenTelemetry::SDK.configure do |c|
      c.add_span_processor(
        OpenTelemetry::SDK::Trace::Export::SimpleSpanProcessor.new(
          OpenTelemetry::SDK::Trace::Export::ConsoleSpanExporter.new
        )
      )
    end
  when "staging"
    # Staging: Console + OTLP
    OpenTelemetry::SDK.configure do |c|
      c.add_span_processor(
        OpenTelemetry::SDK::Trace::Export::SimpleSpanProcessor.new(
          OpenTelemetry::SDK::Trace::Export::ConsoleSpanExporter.new
        )
      )
      # Add OTLP exporter for staging monitoring
    end
  when "production"
    # Production: OTLP only
    OpenTelemetry::SDK.configure do |c|
      # Add OTLP exporter for production monitoring
    end
  else
    # Default: Console exporter
    OpenTelemetry::SDK.configure do |c|
      c.add_span_processor(
        OpenTelemetry::SDK::Trace::Export::SimpleSpanProcessor.new(
          OpenTelemetry::SDK::Trace::Export::ConsoleSpanExporter.new
        )
      )
    end
  end
end

# Set environment and configure
ENV["TRACING_ENV"] = "development"
configure_tracing

# Test the configuration
puts "\n=== Testing Configuration ==="
LlmTracer.workflow(name: "config_test", version: "1.0.0") do |span|
  puts "Created span with environment: #{ENV['TRACING_ENV']}"
  span.set_attribute("config.environment", ENV["TRACING_ENV"])
end

puts "\n=== Configuration examples completed! ==="
puts "Modify the examples above to use your preferred exporters and configuration."
